package NIO;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @author:谢君臣
 * @Date:2021/7/26-10:25
 * @version:1.0
 * @function:
 */
public class Buffer_test {

    public static void main(String []args){
        String str="helloworld";
        // 申请一个非直接缓存区
        Buffer buffer1 = ByteBuffer.allocate(1024);
        System.out.println(buffer1.isDirect());
        // 申请一个直接缓存区
        Buffer buffer2 = ByteBuffer.allocateDirect(1024);
        System.out.println(buffer2.isDirect());
        System.out.println("--------------1------------------");
        //1.分配一个指定大小的缓冲区
        ByteBuffer bb=ByteBuffer.allocate(1024);
        System.out.println(bb.capacity());
        System.out.println(bb.limit());
        System.out.println(bb.position());
        System.out.println("--------------2------------------");
        //2.存入数据
        bb.put(str.getBytes());
        System.out.println(bb.capacity());//位置变为1024
        System.out.println(bb.limit());//位置变为1024
        System.out.println(bb.position());//位置变为10
        System.out.println("---------------3-----------------");
        //3.切换模式数据
        bb.flip();
        System.out.println(bb.capacity());//位置变为1024
        System.out.println(bb.limit());//位置变为10
        System.out.println(bb.position());//位置变为0
        System.out.println("---------------4-----------------");
        //4.读取数据
        byte[] by=new byte[bb.limit()];
        bb.get(by);//获取到缓冲区可读取的所有数据（也就是10）,存放在by数组中
        //System.out.println(by);
        System.out.println(new String(by,0,by.length));
        System.out.println(bb.capacity());
        System.out.println(bb.limit());
        System.out.println(bb.position());
        try {
            bb.get();//再读的话就越界了
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("---------------5-----------------");
        bb.rewind();
        System.out.println(bb.capacity());
        System.out.println(bb.limit());
        System.out.println(bb.position());//位置变为0了，说明又可以读了
        System.out.println("---------------6-----------------");
        //6.clear()：清空缓冲区，但是缓冲区的数据依然存在，但是处于“被遗忘状态”
        bb.clear();
        System.out.println(bb.capacity());
        System.out.println(bb.limit());//指针全部回到最原始状态，不知道有多少数据
        System.out.println(bb.position());
        System.out.println((char)bb.get());
        System.out.println("---------------7-----------------");
        FileOutputStream fos = null;
        FileChannel channel = null;
        try {
            fos = new FileOutputStream("test.txt");
            channel = fos.getChannel();
            ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
            byteBuffer.put("你好，hello word!".getBytes());
            byteBuffer.flip();
            channel.write(byteBuffer);
            channel.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("--------------------------------");
        FileChannel channel1 = null;
        try {
            FileInputStream fis = new FileInputStream("test.txt");
            channel1 = fis.getChannel();
            ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
            int read = channel1.read(byteBuffer);
            String res = new String(byteBuffer.array(),0,read);
            System.out.println(res);
            fis.close();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                channel1.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

}
